layui.config({
	base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	//模糊查询(对药品信息模糊查询)
	$("#search_btn1").click(function(){
		var text=$("#drug_text1").val()
		var newArray = [];
		if($(".search_input").val() != ''){
			var index = layer.msg('查询中，请稍候',{icon: 16,time:false,shade:0.8});
            setTimeout(function(){
            	$.ajax({
					url:"../../drug/tolikeseek",
					type:"get",
					data:"text="+ text,
					dataType : "json",
					success : function(res){
						//循环遍历符合条件的员工的信息
						var str = "";
						for (i = 0; i < res.data.length; i++) {
							str += "<tr>\n" +
								"\t\t\t\t\t\t\t<td><input name=\"checked\" lay-skin=\"primary\" lay-filter=\"choose\" type=\"checkbox\" value='" + res.data[i].id + "'>\n" +
								"\t\t\t\t\t\t\t\t<div class=\"layui-unselect layui-form-checkbox\" lay-skin=\"primary\"><i class=\"layui-icon\"></i></div>\n" +
								"\t\t\t\t\t\t\t</td>"
							str += "<td align=\"center\">" + res.data[i].name + "</td>";
							str += "<td>" + res.data[i].validity + "</td>";
							str += "<td>" + res.data[i].dosageform + "</td>";
							str += "<td>" + res.data[i].storename + "</td>";
							str += "<td>" + res.data[i].sales + "</td>";
							str += "<td>" + res.data[i].price + "</td>";
							if (res.data[i].picture==null||res.data[i].picture=='') {
								str += "<td style='color: red'>" + "暂无图片" + "</td>";
							} else {
								str+="<td>"+"<img src='http://127.0.0.1:8080/userimg/drug/"+res.data[i].picture+"' width='100px' height='100px'/>"+"</td>"
							}
							str += "<td>" + res.data[i].type + "</td>";






							str += "<td>\n" +
								"\t\t\t\t\t\t\t<a class=\"layui-btn layui-btn-mini news_edit\" onclick=\"toupdate(" + res.data[i].id + ")\"><i class=\"iconfont icon-edit\"></i> 编辑</a>\n" +
								"\t\t\t\t\t\t\t<a class=\"layui-btn layui-btn-danger layui-btn-mini news_del\" data-id=\"6\" onclick=\"toremove(" + res.data[i].id + ")\"><i class=\"layui-icon\"></i> 删除</a>\n" +
								"\t\t\t\t\t\t</td>";



							layui.use(['form', 'upload'], function () {
								var form = layui.form();
								form.render();
							})
						}


						$("#cxtable").html(str);
						/*重新渲染*/
						layui.use(['form','upload'],function() {
							var form = layui.form();
							form.render();
						})



					}
				})
            	
                layer.close(index);
            },2000);
		}else{
			layer.msg("请输入需要查询的内容");
		}
	})



	//添加员工操作
	//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
	$(window).one("resize",function(){
		$(".newsAddb_tn1").click(function(){
			var index = layui.layer.open({
				title : "添加药品",
				type : 2,
				content : "druginfo.html",
				success : function(layero, index){
					setTimeout(function(){
						layui.layer.tips('点击此处返回供货商列表', '.layui-layer-setwin .layui-layer-close', {
							tips: 3
						});
					},500)



				}
			})			
			layui.layer.full(index);
		})
	}).resize();



	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});


	function newsList(that){
		//渲染数据
		function renderDate(data,curr){
			var dataHtml = '';
			if(!that){
				currData = newsData.concat().splice(curr*nums-nums, nums);
			}else{
				currData = that.concat().splice(curr*nums-nums, nums);
			}
			if(currData.length != 0){
				for(var i=0;i<currData.length;i++){
					dataHtml += '<tr>'
			    	+'<td><input type="checkbox" name="checked" lay-skin="primary" lay-filter="choose"></td>'
			    	+'<td align="left">'+currData[i].newsName+'</td>'
			    	+'<td>'+currData[i].newsAuthor+'</td>';
			    	if(currData[i].newsStatus == "待审核"){
			    		dataHtml += '<td style="color:#f00">'+currData[i].newsStatus+'</td>';
			    	}else{
			    		dataHtml += '<td>'+currData[i].newsStatus+'</td>';
			    	}
			    	dataHtml += '<td>'+currData[i].newsLook+'</td>'
			    	+'<td><input type="checkbox" name="show" lay-skin="switch" lay-text="是|否" lay-filter="isShow"'+currData[i].isShow+'></td>'
			    	+'<td>'+currData[i].newsTime+'</td>'
			    	+'<td>'
					+  '<a class="layui-btn layui-btn-mini news_edit"><i class="iconfont icon-edit"></i> 编辑</a>'
					+  '<a class="layui-btn layui-btn-normal layui-btn-mini news_collect"><i class="layui-icon">&#xe600;</i> 收藏</a>'
					+  '<a class="layui-btn layui-btn-danger layui-btn-mini news_del" data-id="'+data[i].newsId+'"><i class="layui-icon">&#xe640;</i> 删除</a>'
			        +'</td>'
			    	+'</tr>';
				}
			}else{
				dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
			}
		    return dataHtml;
		}

		//分页
		var nums = 13; //每页出现的数据量
		if(that){
			newsData = that;
		}
		laypage({
			cont : "page",
			pages : Math.ceil(newsData.length/nums),
			jump : function(obj){
				$(".news_content").html(renderDate(newsData,obj.curr));
				$('.news_list thead input[type="checkbox"]').prop("checked",false);
		    	form.render();
			}
		})
	}
})
